layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', 'zmnSelect'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    var vo = json.vo;
    let zmnSelect = layui.zmnSelect;

    let zmnTable = table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page:true,
        limit: vo.pageSize,
        limits: [50, 100],
        height: 'full-140',
        defaultToolbar: [],
        done: function (res, curr, count) {
            //添加按钮
            toolbarRightAddButton([{
                title: '帮助',
                layEvent: 'tableHelp',
                icon: 'layui-icon-help'
            }]);

        }
    });

    laypage.render({
        elem: 'layui-table-page1',
        limit: vo.pageSize,
        count: vo.totalCount,
        curr: vo.pageIndex,
        jump: function(obj, first){
            //obj包含了当前分页的所有参数，比如：
            console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            console.log(obj.limit); //得到每页显示的条数
            if(!first) {
                location.href = '/conf/distribute/company/list.action?plat=' + vo.plat + '&bizType=' + vo.bizType + '&pageIndex=' + obj.curr;
            }
        }
    });

    function getSearchParams(){
        location.href = '/conf/distribute/company/list.action?plat='+vo.plat+'&bizType=' + vo.bizType + '&pageIndex=';
    }

    // 表格内操作按钮
    table.on('tool(zmn-table)', function (obj) {
        var data = obj.data;

        var layEvent = obj.event;
        if (layEvent === 'modifyConfig') {
            zmnLayUtil.openIframeCustom(layer, OMS_BASE_URL + '/conf/distribute/company/edit?weightId=' + data.weightId+ '&configId=' + data.configId,
                {
                    title: '修改配置',
                    shade: 0.4,
                    area: ['80%', '80%']
                }
            );
        }
    });

    function toolbarRightAddButton(layout) {
        let iconElem = [];
        layout.filter((i, v) => i.display || i.display === undefined).forEach((item, index, array) => {
            iconElem.push('<div class="layui-inline" title="' + item.title + '" lay-event="' + item.layEvent + '">'
                + '<i class="layui-icon ' + item.icon + '"></i>'
                + '</div>');
        });
        //  替换容器内容
        document.querySelector('.layui-table-tool-self').innerHTML = iconElem.join('');
    }

    // 表格操作按钮
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event !== "closeBackDoor" && obj.event !== "closeAutoNew" && obj.event !== "closeAutoRework"
            && obj.event !== "openBackDoor" && obj.event !== "openAutoNew" && obj.event !== "openAutoRework" && obj.event !== "addConfig"
            && obj.event !== "tableHelp") {
            return;
        }
        if (obj.event === "addConfig") {
            zmnLayUtil.openIframeCustom(layer, OMS_BASE_URL + '/conf/distribute/company/add?plat=' + vo.plat + '&bizType=1',
                {
                    title: '添加配置',
                    shade: 0.4,
                    area: ['80%', '80%']
                }
            );
            return false;
        }

        if (obj.event == 'tableHelp') {
                tips(help_content.innerHTML, this);
        }

    });

    function tips(html, ele) {
        layui.layer.open({
            type: 4
            , closeBtn: false
            , shade: 0.0001
            , tips: 1
            , skin: 'zmn-tips'
            , shadeClose: true
            , content: [html, ele]
        });
    }

    form.on('switch(changeRobOrderStatus)', function(data){
        let _this = this;
        var value = data.elem.checked ? 2 : 1;
        var ids = new Array();
        ids[0] = data.elem.id;
        layer.alert('你确定要修改抢单状态吗', {
            skin: 'layui-layer-molv'
            , closeBtn: 1
            , anim: 1
            , btn: ['确定', '取消']
            , icon: 6
            , yes: function () {
                zmnOmsPage.ajaxSimple("/conf/distribute/updateRobOrderStatus", {idList: ids.join(","), status: value}, 1000);
            }
            , btn2: function () {
                layer.msg('您取消了状态修改');
                _this.checked = value !== 2;
                layui.form.render();
            },
            cancel: function (index) {
                layer.close(index);
                _this.checked = value !== 2;
                form.render();
            }
        });
    });



    // 公司城市两级联动
    // form.on("select(companyId)", function (obj) {
    //     if (zmnUtil.isNull(obj) || obj.value == 0) {
    //         return layer.alert("请选择！");
    //     }
    //
    //     $.ajax({
    //         async: false,
    //         type: "GET",
    //         url: "/conf/distribute/loadCityByCompanyId.action",
    //         dataType: "json",
    //         data: {companyId: obj.value, bizType: 1},
    //         success: function (items) {
    //             var optionHtml = '<option value="">选择</option>';
    //             for (var i = 0; i < items.length; i++) {
    //                 optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
    //             }
    //             $("#cityId").html(optionHtml);
    //             form.render("select");
    //         }
    //     });
    //
    // });

    zmnSelect.searchSpCompany('companyId', {
        isRadio: true,
        remote: {
            events: {
                extendParams: function () {
                    return {
                        plat: json.vo.plat,
                        type: 10
                    }
                }
            },
        },
        events: {
            changed: function (values) {
                console.log("==========spCompanyIds=========");
                console.log("已选中的值:" + values);

                json.spCompanyId = zmnUtil.isArray(values) && values.length > 0 ? values[0] : null;

                if (json.spCompanyId == null) {
                    return;
                }

                $.ajax({
                    async: false,
                    type: "GET",
                    url: "/conf/distribute/loadCityByCompanyId.action",
                    dataType: "json",
                    data: {companyId: json.spCompanyId, bizType: json.vo.bizType},
                    success: function (items) {
                        var optionHtml = '<option value="">选择</option>';
                        for (var i = 0; i < items.length; i++) {
                            optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                        }
                        $("#cityId").html(optionHtml);
                        form.render("select");
                    }
                });

            }
        }
    });

    //对外暴露的接口
    exports('oms_conf_distribute_company_list', {});
});